package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.RoleDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/2/24:21:35
 * <P> Description:
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired(required = false)
    RoleDao roleDao;

    @Override
    public ResponseEntity findAll(PageEntity01  pageEntity) {
        PageHelper.startPage(pageEntity.getCurrentPage(),pageEntity.getPageSize());
        List<RbacRole> roles = roleDao.findAll();

        if (roles.size() > 0) {
            PageInfo<RbacRole> of = PageInfo.of(roles);
            return new ResponseEntity(200, "查询角色成功", of);
        } else {
            return new ResponseEntity(500, "查询角色失败", null);
        }

    }

    @Override
    @Transactional
    public ResponseEntity updateRole(UpRoleDTO upRoleDTO) {
        int num = roleDao.delById(upRoleDTO.getRoleId());

        num = roleDao.updateRole(upRoleDTO);
        if(num > 0){
            return new ResponseEntity(200,"更新成功",null);
        }else {
            return new ResponseEntity(500,"更新失败",null);
        }
    }

    @Override
    public ResponseEntity findRole(Integer rowId) {
        List<Integer> roleIds = roleDao.findById(rowId);
        if (roleIds.size() > 0) {

            return new ResponseEntity(200, "查询角色权限成功", roleIds);
        } else {
            return new ResponseEntity(500, "查询角色权限失败", null);
        }
    }
}

